< previous page page_414 next page >

Page 414
In the example shown in Figure 18.6, the main form, frmMain, is used to retrieve information from the database. If users do not know a valid value for the Employee ID field, they can click the Search button next to the field to display the second form, frmSearch. The second form comes up with a valid ID value and returns it to the main form. At first glance, the code to manage the transition between forms is easy to understand.
The Search button on the first form simply shows the second form:
Sub cmdSearch_OnClick()
    frmSearch.Show
End Sub
The second form, after performing the search function, places the valid value in a text box on the first form and then unloads itself:
Sub cmdOK_OnClick()
    frmMain.txtID = sSearchResult
    Unload Me
End Sub
Although the simple code listed here would get the job done, it has a couple of basic design problems. First, it treats frmSearch like a totally independent form. The users could return to frmMain without closing frmSearch, either on purpose or by accident, and an orphan form would be left open. If frmMain was later unloaded and frmSearch tried to access it, an error would occur. The second problem with this arrangement is that the code is hard-wired to these two specific forms, so reusing frmSearch in other parts of the application would be difficult. You can solve both of these problems by showing frmSearch modally.
Modal forms, as you may recall, keep the application focus until you hide them. In other words, you cannot switch from a modal form to another form without first closing the modal form. In addition, any code that would have been executed after the Show method is suspended until the modal form is hidden.
sarrow.gifSee "Form Properties Revisited," p. 52
Showing frmSearch modally prevents the users from switching back to frmMain without first closing frmSearch. It also stops code execution in other modules while frmSearch is visible. You can use this capability to your advantage and consolidate some of the code. Consider the following search function that returns the value to the calling procedure rather than to any specific form:
Function sGetValidValue() As String
    frmSearch.Show vbModal
    sGetValidValue = frmSearch.sSearchResult
    Unload frmSearch
End Function
You can now call the preceding function from frmMain (or any other form) to display frmSearch and retrieve a valid search value:
Sub cmdSearch_OnClick()
    frmMain.txtID = sGetValidValue()
End Sub

 
< previous page page_414 next page >